#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Currently this file is used by one python file here. That file is:-
#1. compile_vPath_dissector.py
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#importing system files
import os
import subprocess

#----------------------------------------------------------------------------
#This method cleans the previous built binaries and files from the
#source directory. If there is an error, then that errorcode is
#returned. In case of successfull running of the command, retcode="0"
#is returned back.

def clean_dist():
    retcode=subprocess.Popen(["make","dist","clean"],
                            shell=False,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    retcode.wait()
    return retcode

#----------------------------------------------------------------------------
#This function runs the configure command with the destination_path
#for binaries as input. It opens a file and stores all the log
#messages in that file named "configure.log". If there is an error in
#creating the file, then it returns an errorcode "-2000". If file is
#created successfully, then it runs the configure command and the
#returncode is returned back. If it is a successfull execution, then
#returncode "0" is returned, otherwise errorcode of error which
#occurred is returned.

def configure_with_prefix(path_binaries,code_dir,path_logs,exec_env):
    configure_path=path_logs+"/"+code_dir+"/configure.log"
    fd=0
    try:
        fd=open(configure_path,"w")
    except:
        print "Error in creating log file at path %s for %s system!!!"\
 % (configure_path,exec_env)
        return(-2000)
    configure_path=path_binaries+"/"+code_dir
    retcode=subprocess.Popen(['./configure','--prefix',configure_path],
                            shell=False,
                            stdout=fd,
                            stderr=fd)
    retcode.wait()
    #Running the configure command with the path of output directory
    #specified....
    fd.close()    
    return retcode.returncode

#----------------------------------------------------------------------------
#This function runs the make command. The code directory is given as
#an input to this method. It opens a file and stores all the log
#messages in that file named "make.log". If there is an error in
#creating the file, then it returns an errorcode "-2000". If file is
#created successfully, then it runs the make command and the
#returncode is returned back. If it is a successfull execution, then
#returncode "0" is returned, otherwise errorcode of error which
#occurred is returned.

def make(code_dir,path_logs,exec_env):
    make_path=path_logs+"/"+code_dir+"/make.log"
    fd_make=0
    try:
        fd_make=open(make_path,"w")  #Opening a file where all the
                                     #logs to the below command will
                                     #be written....
    except:
        print "Error in creating log file at path %s for %s system!!!"\
 % (make_path,exec_env)
        return(-2000)      #Errorcode of -2000 if there is an error in
                           #creating the file.....
    
    #Running the command to perform the required operation.....
    ret_make=subprocess.Popen(['make'],
                            shell=False,
                            stdout=fd_make,
                            stderr=fd_make)
    ret_make.wait()
    fd_make.close()
    return ret_make.returncode

#----------------------------------------------------------------------------
#This function runs the make_install command. The code directory is
#given as an input to this method. It opens a file and stores all the
#log messages in that file named "make_install.log". If there is an
#error in creating the file, then it returns an errorcode "-2000". If
#file is created successfully, then it runs the make command and the
#returncode is returned back. If it is a successfull execution, then
#returncode "0" is returned, otherwise errorcode of error which
#occurred is returned.

def make_install(code_dir,path_logs,exec_env):
    make_install_path=path_logs+"/"+code_dir+"/make_install.log"
    fd_make_install=0
    try:
        fd_make_install=open(make_install_path,"w")  #Opening a file
                                                     #where all the
                                                     #logs to the
                                                     #below command
                                                     #will be
                                                     #written....
    except:
        print "Error in creating log file at path %s for %s system!!!"\
 % (make_install_path,exec_env)
        return(-2000)        #Errorcode of -2000 if there is an error
                             #in creating the file.....
    
    #Running the command to perform the specified task.....
    ret_make_install=subprocess.Popen(['make','install'],
                                    shell=False,
                                    stdout=fd_make_install,
                                    stderr=fd_make_install)
    ret_make_install.wait()
    fd_make_install.close()
    return ret_make_install.returncode

#----------------------------------------------------------------------------
